﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class DuplicateExperiment : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (First Time or Post Back).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] == null || (Request.QueryString["ID"] != null && BEELab.Logic.ProgramManagement.ExperimentManagement.GetOwnerID(int.Parse(Request.QueryString["ID"].ToString())) != int.Parse(Session["UserID"].ToString()) && (Session["Admin"] == null || (bool)Session["Admin"] == false)))
            {
                Response.Redirect("Main.aspx");
            }

            if (!IsPostBack && Request.QueryString["ID"] != null)
            {
               
            }
            else
            {
                literal1.Text = "<script>parent.$.fancybox.close(); window.parent.location.reload();    </script>";
            }
        }

        /// <summary>
        /// Occures once presiing the "duplicate experiment" button.
        /// Will create a duplication of existing experiment, 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            //If an Admin is commiting the operation - will create duplication with ownerID that 
            //the Admin has choose from DropDown list
            if (Session["Admin"] != null && (bool)Session["Admin"] == true)
            {
                int newID = BEELab.Logic.ProgramManagement.ExperimentManagement.DuplicateExperiment(int.Parse(Request.QueryString["ID"].ToString()), txtName.Text, BEELab.Logic.ProgramManagement.ExperimentManagement.GetDescription(int.Parse(Request.QueryString["ID"].ToString())), int.Parse(ddlOwner.SelectedValue), true);
            }
            //if the operation is commited by regular user - will create duplication of the
            //experiment with current user as ownerID
            else
            {
                int newID = BEELab.Logic.ProgramManagement.ExperimentManagement.DuplicateExperiment(int.Parse(Request.QueryString["ID"].ToString()), txtName.Text, BEELab.Logic.ProgramManagement.ExperimentManagement.GetDescription(int.Parse(Request.QueryString["ID"].ToString())), int.Parse(Session["UserID"].ToString()), true);
            }
        }
    }
}